package com.business.dynamic.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.business.common.dto.R;
import com.business.dynamic.entity.DynamicMessage;
import com.business.dynamic.service.DynamicMessageService;
import com.business.dynamic.utils.PageFactory;
import com.business.redis.ExpiresTimeEnum;
import com.business.redis.constant.RedisKeyConstant;
import com.business.redis.utils.RedisKeysUtil;
import com.business.redis.utils.RedisUtil;
import com.business.user.annotation.TokenPermission;
import com.business.user.entity.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 动态评论或者赞消息表: 后端controller类
 *
 * @author chenjiehong
 * @date 2019-12-02 17:55:03
 * @since 1.0
 */
@RestController
@RequestMapping(value = "/message")
@TokenPermission
public class DynamicMessageController {


    @Autowired
    private DynamicMessageService dynamicMessageServiceImpl;


    /**
     * 更新所以未读的消息表
     *
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/updateAll")
    @ResponseBody
    public R updateAll(@RequestBody JSONObject params, UserDto userDto) {
        Integer type = params.getIntValue("type");
        Integer r = dynamicMessageServiceImpl.updateAll(userDto.getId(), type);
        Set<String> keys = new HashSet<>();
        String countByTypeKey = String.format(RedisKeyConstant.DYNAMIC_MESSAGE_COUNT_BY_TYPE, userDto.getId());
        String countKey = String.format(RedisKeyConstant.DYNAMIC_MESSAGE_COUNT, userDto.getId());
        keys.add(countByTypeKey);
        keys.add(countKey);
        RedisUtil.delValue(keys);
        return R.success();
    }

    /**
     * 分页查询消息表
     *
     * @param params
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/queryPage")
    @ResponseBody
    public R queryPage(@RequestBody JSONObject params, UserDto userDto) {
        Page<DynamicMessage> page = new PageFactory<DynamicMessage>().defaultPage(params);
        Integer type = params.getInteger("type");
        Integer isAll = params.getInteger("isAll");
        return R.success(dynamicMessageServiceImpl.queryPage(page, userDto.getId(), type, isAll));
    }


    /**
     * 查询未读总数
     *
     * @param user
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/getCount")
    @ResponseBody
    @TokenPermission
    public R getCount(UserDto user) {
        String key = String.format(RedisKeyConstant.DYNAMIC_MESSAGE_COUNT, user.getId());
        Object count = null;
        if (RedisUtil.hasKey(key)) {
            count = RedisUtil.getValue(key);
        } else {
            count = dynamicMessageServiceImpl.getCount(user.getId());
            RedisUtil.setValue(key, count, ExpiresTimeEnum.LEVEL_2.getExpiresTime());
        }
        return R.success(count);
    }

    /**
     * 查询未读总数
     *
     * @param user
     * @date 2019-11-29 14:28:27
     */
    @PostMapping("/getCountByType")
    @ResponseBody
    @TokenPermission
    public R getCountByType(UserDto user) {
        String key = String.format(RedisKeyConstant.DYNAMIC_MESSAGE_COUNT_BY_TYPE, user.getId());
        Map<String, Integer> resultMap = new HashMap<>();
        if (RedisUtil.hasKey(key)) {
            Set<String> keys = RedisUtil.getHashKeys(key);
            Iterator<String> iterator = keys.iterator();
            while (iterator.hasNext()) {
                String type = iterator.next();
                resultMap.put(type, Integer.parseInt(RedisUtil.getHashValue(key, type) + ""));
            }
        } else {
            List<Map<String, Integer>> mapList = dynamicMessageServiceImpl.getCountByType(user.getId());
            for (Map<String, Integer> map : mapList) {
                Integer type = map.get("type");
                Integer value = map.get("value");
                resultMap.put(type + "", value);
                RedisUtil.setHashValue(key, type + "", value);
            }
            RedisUtil.expire(key, ExpiresTimeEnum.LEVEL_1.getExpiresTime());
        }
        return R.success(resultMap);
    }

}